package com.sensePlusVR.activity.mine;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.xutils.x;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import android.content.DialogInterface;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sensePlusVR.activity.guesture.GestureEditActivity;
import com.sensePlusVR.activity.guesture.GestureVerifyActivity;
import com.sensePlusVR.activity.user.ResetPwdActivity;
import com.sensePlusVR.base.BaseActivity;
import com.sensePlusVR.bean.AppVersionInfo;
import com.sensePlusVR.bean.EventBusBean;
import com.sensePlusVR.main.R;
import com.sensePlusVR.net.NetRequestManager;
import com.sensePlusVR.utils.AppManager;
import com.sensePlusVR.utils.CommonData;
import com.sensePlusVR.utils.Constant;
import com.sensePlusVR.utils.DialogUtils;
import com.sensePlusVR.utils.EventBusUtils;
import com.sensePlusVR.utils.GameDownload;
import com.sensePlusVR.utils.MyLog;
import com.sensePlusVR.utils.MyPageJump;
import com.sensePlusVR.utils.MySharedPreferences;
import com.sensePlusVR.utils.MyToast;
import com.sensePlusVR.utils.PerformancetTestUtil;
import com.sensePlusVR.utils.VersionUtil;
import com.sensePlusVR.utils.VersionUtil.CheckVersion;
import com.sensePlusVR.widget.SwitchButtonView;
import com.sensePlusVR.widget.SwitchButtonView.OnToggleChangeListener;
import com.sensePlusVR.widget.dialog.AppVersionTipDialog;
import com.sensePlusVR.widget.dialog.OpenCellularTipDialog;
/**
 * 
 * @author: Loren
 * @create: 2016-8-10下午2:41:46
 * @desc: My-->Setting界面
 *
 */
@ContentView(R.layout.activity_setting_core)
public class CoreSettingActivity extends BaseActivity implements OnClickListener{
	private static final String TAG = CoreSettingActivity.class.getSimpleName();
	@ViewInject(R.id.title_bar_left_ib)
	private ImageButton title_bar_left_ib;
	
	@ViewInject(R.id.title_bar_title_tv)
	private TextView title_bar_title_tv;
	
	@ViewInject(R.id.setting_core_guest_pwd_relative)
	private RelativeLayout setting_core_guest_pwd_relative;
	
	@ViewInject(R.id.setting_core_replace_icon_relative)
	private RelativeLayout setting_core_replace_icon_relative;
	
	@ViewInject(R.id.setting_core_clear_cache_relative)
	private RelativeLayout setting_core_clear_cache_relative;
	
	@ViewInject(R.id.setting_core_checkupdate_relative)
	private RelativeLayout setting_core_checkupdate_relative;
	
	@ViewInject(R.id.setting_core_detect_relative)
	private RelativeLayout setting_core_detect_relative;
	
	@ViewInject(R.id.setting_version_iv)
	private TextView setting_version_iv;
	
	@ViewInject(R.id.setting_update_point)
	private ImageView setting_update_point;
	
	@ViewInject(R.id.setting_core_network_relative)
	private RelativeLayout setting_core_network_relative;
	
	@ViewInject(R.id.setting_core_network_switchBtn)
	private SwitchButtonView setting_core_network_switchBtn;
	
	@ViewInject(R.id.setting_core_pwd_relative)
	private RelativeLayout setting_core_pwd_relative;
	
	@ViewInject(R.id.setting_core_loginout)
	private Button setting_core_loginout;
	
	//版本信息
	//private AppVersionInfo newAppVersionInfo;
	
	//是否可以更新
	//private boolean canUpdateApp = false;
	//当前版本号
	private String curVersionName;
	
	private boolean isCheckUpdataClick = false;
	
	@Override
	public void setMyContentView() {
		x.view().inject(this);
	
	}
	
	
	

	@Override
	public void initView() {
		super.initView();
		AppManager.getInstance().addActivity(this);
		title_bar_title_tv.setText(R.string.setting_settings);
		
		curVersionName = VersionUtil.getVersionName(this);
		MyLog.d(TAG, "curVersionName : " + curVersionName);
		setting_version_iv.setText(curVersionName);
		//newAppVersionInfo=((VRApplication)getApplication()).getNewVersionInfo();
//		if(newAppVersionInfo!=null){
//			canUpdateApp = VersionUtil.canVersionUpdate(newAppVersionInfo.getVersonname(), curVersionName);	
//			MyLog.d(TAG, "newAppVersion="+newAppVersionInfo.getVersonname()+" curVersion="+curVersionName+" canUpdateApp="+canUpdateApp);
//		}
//		setting_update_point.setVisibility(canUpdateApp?View.VISIBLE:View.INVISIBLE);
		
		title_bar_left_ib.setOnClickListener(this);
		setting_core_guest_pwd_relative.setOnClickListener(this);
		setting_core_replace_icon_relative.setOnClickListener(this);
		setting_core_clear_cache_relative.setOnClickListener(this);
		setting_core_checkupdate_relative.setOnClickListener(this);
		setting_core_pwd_relative.setOnClickListener(this);
		setting_core_detect_relative.setOnClickListener(this);
		setting_core_loginout.setOnClickListener(this);
		
		setting_core_network_switchBtn.setOnToggleChangeListener(myOnToggleChangeListener);
		
	}
	
	@Override
	public void initData() {
		super.initData();
		//设置MobileNetSitch初始值
		boolean isMobileNetOpened = CommonData.getMobileNetSwitch(this);
		setting_core_network_switchBtn.setIsToggleOn(isMobileNetOpened);
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.title_bar_left_ib:
			onBackPressed();
			break;
		case R.id.setting_core_guest_pwd_relative:
			String inputCode = MySharedPreferences.getString2(this,MySharedPreferences.NAME_NOT_CLEAR,Constant.SP_INPUTCODE);
			Log.d(TAG, "input code ： " + inputCode);
			if(inputCode != ""){
				MyPageJump.pageJumpAddBoolean(this, GestureVerifyActivity.class, Constant.INTENT_GUESTURE_FIRST_RESET,true);
			}else{
				MyPageJump.pageJump(this, GestureEditActivity.class);
			}
			break;
		case R.id.setting_core_replace_icon_relative:
			MyPageJump.pageJump(this, ReplaceIconActivity.class);
			break;
		case R.id.setting_core_clear_cache_relative:
			MyPageJump.pageJump(this, ClearCacheActivity.class);
			break;
		case R.id.setting_core_checkupdate_relative:
			isCheckUpdataClick = true;
			VersionUtil.checkoutVersion(this,mCheckVersion);
			break;
		case R.id.setting_core_pwd_relative:
			MyPageJump.pageJump(this, ResetPwdActivity.class);
		break;
		case R.id.setting_core_detect_relative:
			PerformancetTestUtil.startPerformancetTesting(this, Constant.GAME_MODE_VR,true);
			break;
		case R.id.setting_core_loginout:
			exitUserLogin();
			break;
		}
	}
	
	/**
	 * SwitchButton 网络4G开关监听
	 */
	private OnToggleChangeListener myOnToggleChangeListener = new OnToggleChangeListener() {

		@Override
		public void onChange(boolean isToggleOn) {
			MyLog.d(TAG, "onChange isToggleOn : " + isToggleOn);
		}

		@Override
		public void onClick() {
			boolean isMobileNetSwitch = CommonData.getMobileNetSwitch(CoreSettingActivity.this);
			MyLog.d(TAG, "onClick isMobileNetSwitch : " + isMobileNetSwitch);
			if(isMobileNetSwitch){
				//关闭4G网络状态
				saveMobileNetSwitch(false);
			}else{
				//开启4G网络状态(是否开启在OpenCellularTipDialog设置)
				OpenCellularTipDialog openCellularTipDialog = new OpenCellularTipDialog(CoreSettingActivity.this);
				openCellularTipDialog.show();
			}
		}
	};
	
	
	@Subscribe(threadMode = ThreadMode.MAIN)
	public void onMessageEvent(EventBusBean event) {
		String eventMessage = event.getMessage();
		MyLog.d(TAG, "onMessageEvent : " + eventMessage);
		// OpenCellularTipDialog回调
		if (eventMessage.equals(EventBusUtils.EVENT_OPENCELLULARDIALOG_DISMISS)) {
			int type = (Integer) event.getData();
			MyLog.d(TAG, "onMessageEvent type : " + type);
			if (type == OpenCellularTipDialog.TYPE_CELLULAR_OPENED) {

				saveMobileNetSwitch(true);

			} else if (type == OpenCellularTipDialog.TYPE_CELLULAR_CLOSED) {

				saveMobileNetSwitch(false);

			}

		}
	}
	
	/**
	 * 保存当前4G开关状态
	 * @param isMobileNetOpened
	 */
	private void saveMobileNetSwitch(boolean isMobileNetOpened){
		GameDownload.allowCelluerChanged(isMobileNetOpened);
		setting_core_network_switchBtn.setIsToggleOn(isMobileNetOpened);
		CommonData.saveMobileNetSwitch(CoreSettingActivity.this, isMobileNetOpened);
	}
	
	//版本检查回调
		private CheckVersion mCheckVersion = new CheckVersion() {
			
			@Override
			public void onVersionCallback(AppVersionInfo appVersionInfo) {
				
				if(appVersionInfo!=null){
					
					MyLog.d(TAG, appVersionInfo.toString());
					
					boolean canUpdateApp = VersionUtil.canVersionUpdate(appVersionInfo.getVersonname(), curVersionName);	
					
					if(isCheckUpdataClick && canUpdateApp){
						AppVersionTipDialog appVersionTipDlg = new AppVersionTipDialog(CoreSettingActivity.this,appVersionInfo,false,false);
						appVersionTipDlg.show();
					}
					
					if(isCheckUpdataClick && !canUpdateApp){
						MyToast.showMessage(CoreSettingActivity.this,R.string.version_is_latest);
					}
					setting_update_point.setVisibility(canUpdateApp?View.VISIBLE:View.INVISIBLE);
				}
				
				
			}
		};

	/**
	 * 退出用户登录
	 */
	private void exitUserLogin() {
		DialogUtils.showConfirmDialogConfirmCancel(this, getResources().getString(R.string.app_loginout), getResources().getString(R.string.system_alert_quit),
				new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						NetRequestManager
								.startLoginOut(CoreSettingActivity.this);
					}
				}, new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
					}
				});
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		
		EventBus.getDefault().register(this);
		isCheckUpdataClick = false;
		VersionUtil.checkoutVersion(this,mCheckVersion);
		
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		
		EventBus.getDefault().unregister(this);
	}

	
	@Override
	public void onBackPressed() {
		super.onBackPressed();
		AppManager.getInstance().finishActivity(this);
	}
	
	
}
